/*
 * @Autor: Haiting Zhao
 * @Date: 2022-02-26 15:30:04
 * @LastEditors: Haiting Zhao
 * @LastEditTime: 2022-02-26 15:37:31
 * @Description: file content
 * @FilePath: \test\src\crypto\index.js
 */
import CryptoJS from 'crypto-js'
export default {
  /**
   * @description: 加密
   * @param {*} word
   * @param {*} keyStr
   * @return {*}
   * @author: Haiting Zhao
   */
  set (word, keyStr) {
    keyStr = keyStr || 'leagueandstudent' // 16位的密钥，自己定义，和下面的密钥要相同
    var key = CryptoJS.enc.Utf8.parse(keyStr)
    var srcs = CryptoJS.enc.Utf8.parse(word)
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return encrypted.toString()
  },
  /**
   * @description: 解密
   * @param {*} word
   * @param {*} keyStr
   * @return {*}
   * @author: Haiting Zhao
   */
  get (word, keyStr) {
    keyStr = keyStr || 'leagueandstudent'
    var key = CryptoJS.enc.Utf8.parse(keyStr)
    var decrypt = CryptoJS.AES.decrypt(word, key, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  }
}
